package dome_011;

import java.util.Arrays;

/**
 * <p>Description: [线程安全的单例模式
 * 更好的是采用下面的方式，既不用加锁，也能实现懒加载]</p>
 * Created on 2020/12/14
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class Singleton {
    private Singleton() {
        System.out.println("single");
    }

    private static class Inner {
        private static Singleton singleton = new Singleton();
    }

    public static Singleton getSingleton() {
        return Inner.singleton;
    }

    public static void main(String[] args) {
        Thread[] threads = new Thread[200];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                Singleton.getSingleton();
            });
        }

        Arrays.asList(threads).forEach(o -> o.start());
    }
}
